﻿namespace gRPCDemo.CameraModule
{
    public class Client : BLL.ICameraModuleClient
    {
        readonly bool armTest = false;
        public async Task CameraModelTest()
        {
            string url;
            if (armTest)
            {
                url = "http://172.20.60.138:9988";
            }
            else
            {
                url = "https://localhost:9988";
                //url = "http://TP-E14-GUO-U.local:9988";
            }
            var channel = GrpcChannel.ForAddress(url);
            var client = new CameraModel.CameraModelClient(channel);
            var devices = await client.getdevicesAsync(new Google.Protobuf.WellKnownTypes.Empty());
            //await UpdateCameraInfoTest(client, devices.SerialNumbers);
            //await SetDeviceStatusTest(client, devices.SerialNumbers[0], "Connection");
            //await SetDeviceStatusTest(client, devices.SerialNumbers[0], "LoadPar");
            //await SetDeviceStatusTest(client, devices.SerialNumbers[0], "SavePar");
            //await SetDeviceStatusTest(client, devices.SerialNumbers[0], "StartCollecting");
            await SetDeviceStatusTest(client, "", "StartSaveImage");

            //await FeedbackImageTest(client);
            await FeedbackJoinedImageTest(client);
            //await SetDeviceStatusTest(client, devices.SerialNumbers[0], "StopSaveImage");
            //await SetDeviceStatusTest(client, devices.SerialNumbers[0], "StopCollecting");
            await SetDeviceStatusTest(client, devices.SerialNumbers[0], "Disconnect");
        }
        async Task SetDeviceStatusTest(CameraModel.CameraModelClient client, string serialNumber, string cmd)
        {

            DeviceState stateRequest;
            Device device;

            stateRequest = new DeviceState
            {
                State = Enum.Parse<DeviceState.Types.State>(cmd),
                FileTimePath = armTest ? "/home/dmc/CameraModulePics" : @"F:\CameraModel\",
                SerialNumber = serialNumber
            };
            device = await client.setDeviceStatusAsync(stateRequest);
        }
        async Task UpdateCameraInfoTest(CameraModel.CameraModelClient client, IEnumerable<string> serialNumbers)
        {
            await client.UpdateCameraInfoAsync(new CameraInfos
            {
                CameraimageDirection = { "向下" },
                CameraserialNumber = { serialNumbers }
            });
        }

        async Task FeedbackImageTest(CameraModel.CameraModelClient client)
        {
            var stream = client.feedbackImage(new Google.Protobuf.WellKnownTypes.Empty());
            for (int i = 0; i < 10000; i++)
            {
                var response = await stream.ResponseStream.MoveNext();
                if (response)
                {
                    var image = stream.ResponseStream.Current;
                    Console.WriteLine($"Received image with size: {image.Icon.Length} bytes");
                    File.WriteAllBytes($@"E:\CameraModel\Image.jpg", image.Icon.ToByteArray());
                }
            }

        }
        async Task FeedbackJoinedImageTest(CameraModel.CameraModelClient client)
        {
            var stream = client.feedbackJoiningImage(new Google.Protobuf.WellKnownTypes.Empty());
            for (int i = 0; i < 10000; i++)
            {
                var response = await stream.ResponseStream.MoveNext();
                if (response)
                {
                    var image = stream.ResponseStream.Current;
                    Console.WriteLine($"Received joined image with size: {image.Icon.Length} bytes");
                    File.WriteAllBytes($@"E:\CameraModel\JoinedImage.jpg", image.Icon.ToByteArray());
                }
            }
        }
    }
}
